#include "BoardModelling.h"

#include "BoardTextures.h"
#include "../pieces/PiecesTextures.h"
#include "../pieces/PiecesModelling.h"
#include "../frameworks/Drawer.h"
#include <GL/glui.h>


char game_board[BOARD_DIMENSION][BOARD_DIMENSION] = {
	{'0', '0', '0', '0', '0', '0', '0', '6', '0', '0'},
	{'2', '0', '2', '0', '2', '0', '2', '0', '2', '0'},
	{'2', '0', '2', '0', '2', '0', '2', '0', '2', '0'},
	{'2', '0', '2', '0', '2', '0', '2', '0', '2', '0'},
	{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
	{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
	{'0', '1', '0', '1', '0', '1', '0', '1', '0', '1'},
	{'0', '1', '0', '1', '0', '1', '0', '1', '0', '1'},
	{'0', '1', '0', '1', '0', '1', '0', '1', '0', '1'},
	{'0', '0', '5', '0', '0', '0', '0', '0', '0', '0'},
};

void draw_bench_board() {
	glPushMatrix();
		glScalef(BENCH_BOARD_SCALE_X, BENCH_BOARD_SCALE_Y, BENCH_BOARD_SCALE_Z);
		draw_cube(active_bench_board_texs.ids, 1);
	glPopMatrix();
}

void draw_dummy_square() {
	glPushMatrix();
		glTranslatef(DUMMY_SQUARE_DIM / 2.0, +0.5, DUMMY_SQUARE_DIM / 2.0);
		glScalef(DUMMY_SQUARE_SCALE_XZ, 1.0, DUMMY_SQUARE_SCALE_XZ);
		glRotatef(-90, 1.0, 0.0, 0.0);
		glNormal3f(0.0, 1.0, 0.0);
		drawRect();
	glPopMatrix();
}

void draw_dummy_squares_and_pieces(GLUquadric *glQ, GLenum mode) {
	int nsquares = BOARD_DIMENSION * BOARD_DIMENSION,
		square_counter = 0;
	double dx = BOARD_PIECES_DIM / (double) BOARD_DIMENSION,
		   dz = BOARD_PIECES_DIM / (double) BOARD_DIMENSION;
	double init_posx = - (BOARD_PIECES_DIM / 2.0),
		   init_posz = - (BOARD_PIECES_DIM / 2.0);
	double posx = init_posx,
		   posz = init_posz,
		   posy = BOARD_DIM_Y / 2.0;

	glPushMatrix();
		for(int zi = 0; zi < BOARD_DIMENSION; ++zi, posx = init_posx, posz += dz) {
			for(int xi = 0; xi < BOARD_DIMENSION; ++xi, posx +=dx, ++square_counter) {
				if( mode == GL_SELECT )
					glLoadName(square_counter);
				glPushMatrix();
					glTranslatef(posx, posy, posz);
					if( mode == GL_SELECT )
						draw_dummy_square();
					switch( game_board[zi][xi] ) {
						case '1':
							draw_soldier(glQ, TEXID_SOLDIER_P1);
							break;
						case '2':
							draw_soldier(glQ, TEXID_SOLDIER_P2);
							break;
						case '5':
							draw_house(castle_p1_texs);
							break;
						case '6':
							draw_house(castle_p2_texs);
							break;
						default:
							break;
					}
				glPopMatrix();
			}
		}
	glPopMatrix();
}

void display_boards(GLUquadric *glQ) {

	// Main Board Cube
	glPushMatrix();
		glScalef(BOARD_SCALE_XZ, BOARD_SCALE_Y, BOARD_SCALE_XZ);
		draw_cube(active_main_board_texs.ids, 1);
	glPopMatrix();
	
	// Main Board Surface w/ evaluator
	glPushMatrix();
		glTranslatef(0.0, BOARD_DIM_Y/2, 0.0);
		draw_board_surface(active_main_board_texs.ids[0], MAIN_BOARD);
	glPopMatrix();

	// Bench Board 1
	
	glPushMatrix();
		glTranslatef(BENCH_BOARD_TRANSLATE_X, BENCH_BOARD_TRANSLATE_Y, BENCH_BOARD_TRANSLATE_Z);
		draw_bench_board();
	glPopMatrix();

	// Bench Board 1 Surface w/ evaluator
	glPushMatrix();
		glTranslatef(BENCH_BOARD_TRANSLATE_X, 0, BENCH_BOARD_TRANSLATE_Z);
		draw_board_surface(active_bench_board_texs.ids[0], BENCH_BOARD1);
	glPopMatrix();
		
	// Bench Board 2
	glPushMatrix();
		glTranslatef(BENCH_BOARD_TRANSLATE_X, BENCH_BOARD_TRANSLATE_Y, -BENCH_BOARD_TRANSLATE_Z);
		draw_bench_board();
	glPopMatrix();

	// Bench Board 2 Surface w/ evaluator
	
	glPushMatrix();
		glTranslatef(BENCH_BOARD_TRANSLATE_X, 0, -BENCH_BOARD_TRANSLATE_Z);
		draw_board_surface(active_bench_board_texs.ids[0], BENCH_BOARD2);
	glPopMatrix();
}